package com.eland.oa.main.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eland.oa.main.constant.ELOpCode;
import com.eland.oa.main.controller.util.ContextResolver;
import com.eland.oa.main.model.ELOpRtnPack;
import com.eland.oa.main.service.LoginDEPService;

@Controller
@RequestMapping("/main.htm")
public class MainController {

	@Autowired @Qualifier("main-service-login")
	public LoginDEPService loginService;
	
	@RequestMapping(method= RequestMethod.GET)
	public ModelAndView mainHandler(@RequestParam(required=false) String contextCode) {
		Map<String, Object> model = new HashMap<String, Object>();
		
		//Check if user is logged in, send login view if 
		if (!loginService.isUserLoggedIn()) {
			String code = ELOpRtnPack.resolveReturnCode(
					ELOpCode.TYP_ERROR, 
					ELOpCode.OP_SYS_LOGIN, 
					ELOpCode.MSG_LOGIN_E_USER_NOT_AUTHCED);
			return new ModelAndView("redirect:/login.htm?contextCode=" + code + "&redirect=main.htm");
		}
		
		return new ModelAndView("main", "model", model);
	}
}
